__author__ = 'Fre'

from numpy import *
from math import pi
import numpy.linalg

"""
random
"""
# generating a random overdetermined system
#A = random.rand(2,2)
#b = random.rand(2,1)

#A = [[2, 4], [1, 4], [3, 3]]
#b = [[8], [6], [9.5]]
#
#lstsq = linalg.lstsq(A,b)[0] # computing the numpy solution

#Q,R = linalg.qr(A) # qr decomposition of A
#Qb = dot(Q.T,b) # computing Q^T*b (project b onto the range of A)
#x_qr = linalg.solve(R,Qb) # solving R*x = Q^T*b

# comparing the solutions
#print ('qr solution')
#print (x_qr)
#print ('lstqs solution')
#print (x_lstsq)

"""
input user
"""

aX = float(input("x co van A: "))
aY = float(input ("y co van A: "))
bX = float(input("x co van B: "))
bY = float(input ("y co van B: "))
cX = float(input ("x co van C: "))
cY = float(input ("y co van C: "))
hoekAP = float(input("hoek AP: "))
hoekBP = float(input("hoek BP: "))
hoekCP = float(input("hoek CP: "))
hoekAP=2*pi*(hoekAP)/360
hoekBP=2*pi*(hoekBP)/360
hoekCP=2*pi*(hoekCP)/360

a11 = 1
a12 = -numpy.tan(hoekAP)
a21 = 1
a22 = -numpy.tan(hoekBP)
a31 = 1
a32 = -numpy.tan(hoekCP)
b11 = aX - aY * numpy.tan(hoekAP)
b21 = bX - bY * numpy.tan(hoekBP)
b31 = cX - cY * numpy.tan(hoekCP)

matrixA = [[a11, a12], [a21, a22], [a31, a32]]
vectorB = [[b11], [b21], [b31]]

lstsq = linalg.lstsq(matrixA, vectorB)[0]
print (lstsq)

import matplotlib.pyplot as plt

def func(a, x, b):
    return a*x+b

#plt.plot([1,5])
#plt.show()
#plt.plot([5, 10], [8, 6], 'ro')
#plt.axis([0, 20, 0, 20])
#plt.show()
xrange = numpy.arange(0., 15., 1.)
#plt.plot(xrange, func(1, xrange, 5))
plt.plot([aX, bX, cX], [aY, bY, cY],'ro')
plt.plot([lstsq[0]], [lstsq[1]],'bo')
plt.text(aX +0.5, aY+0.5, 'A')
#scanner object maken, met naam als property
plt.plot([lstsq[0]], [lstsq[1]])
#slope = (lstsq[1] - aY)/(lstsq[0] - aX)
slope = numpy.tan((pi/2)-hoekAP)
plt.plot(xrange, func(slope, xrange, aY - slope *aX))
#slope = (lstsq[1] - bY)/(lstsq[0] - bX)
slope = numpy.tan((pi/2)-hoekBP)
plt.plot(xrange, func(slope, xrange, bY - slope *bX))
#slope = (lstsq[1] - cY)/(lstsq[0] - cX)
slope = numpy.tan((pi/2)-hoekCP)
plt.plot(xrange, func(slope, xrange, cY - slope *cX))
plt.axis([-10, 20, -10, 20])
plt.show()

"""
tkinter test
"""

#print('hello world')
#
##from tkinter import *
##class App:
##    def __init__(self, master):
##        frame = Frame(master)
##        frame.pack()
##        self.button = Button(frame, text="QUIT", fg="red", command=frame.quit)
##        self.button.pack(side=LEFT)
##        self.hi_there = Button(frame, text="Hello", command=self.say_hi)
##        self.hi_there.pack(side=LEFT)
##
##    def say_hi(self):
##        print ("hi there, everyone!")
##
##root = Tk()
##app = App(root)
##root.mainloop()

